package js;

import java.io.File;
import java.io.FileInputStream;

import com.xucia.strands.Strands;

import titt.TITT;
import titt.TITTNuder;
import titt.io.IOFactory;
import titt.server.RunServer;
import titt.service.content.Mimes;
import titt.service.content.NuderResponse;

public class Dev {
	
	public NuderResponse get(String path) throws Exception {
		File f = new File("src/js/"+path);
				
		if(f.getName().equals("titt.js"))
			return new NuderResponse(Strands.compileTITT(new File("titt.js")), Mimes.JAVASCRIPT);
		
		if(f.exists() && f.getName().endsWith(".js"))
			return new NuderResponse(Strands.compileFile(f.getAbsolutePath()), Mimes.JAVASCRIPT);
		
		if(f.exists())
			return new NuderResponse(f, Mimes.getContentType(f.getName()));
		
		
		f = new File("src/js/index.html");
		byte[] data = new byte[(int)f.length()];
		
		IOFactory.createInputStream(new FileInputStream(f)).read(data);
		
		return new NuderResponse(new String(data).replace("$ID", path.substring(1)), Mimes.getContentType(f.getName()));
		
	}
	
	public static void main(String[] args) {
		
		RunServer.run(8080, "localhost");

		TITTNuder.register(new Dev(), "*");
		TITTNuder.register(new Nuder(), "test");
		
		TITT.register(new Foo(), "foo");
		
	}
	
}
